package transfer;

import java.io.File;

public class Transfer {

	/**
	 * @param args
	 */

	// public final static String FROM_LOG_FOLDER = "E:/WireSharkLog/19052010/";
	public static String FROM_LOG_FOLDER = "C:/eclipseProjects/securityProject/files/from/";
	public static String TO_LOG_FOLDER = "C:/eclipseProjects/securityProject/files/to/";
	private static long lastModifiedFileTime = 0;

	public static void main(String[] args) {

		if ((args.length < 3) || (null == args[0]) || (null == args[1]))
			errorInArguments();
		
		// if sender
		if (args[0].equals("0")) {
			try {
				FROM_LOG_FOLDER = args[1];
				long lengthOfFileBeforeSending = Long.valueOf(args[2]); 
				FileTransferConnection.getInstance().connect(true, "");

				File logFolder = new File(FROM_LOG_FOLDER);
				long curFolderLastModifiedTime;
				long curFileLastModifiedTime;
				boolean wasSent;

				while (true) {
					// if (FileTransferConnection.getInstance().isSocketAlive())
					// {
					// System.out.println("Alive");
					// } else {
					// System.out.println("Dead");
					// }
					curFolderLastModifiedTime = lastModifiedFileTime;
					for (File logfile : logFolder.listFiles()) {
						curFileLastModifiedTime = logfile.lastModified();
						if (curFileLastModifiedTime > curFolderLastModifiedTime) {
							if (curFileLastModifiedTime > lastModifiedFileTime) {
								while (logfile.length() < lengthOfFileBeforeSending){
									System.out.println("Waiting for file to reach lenght " + lengthOfFileBeforeSending + " before sending it");
									Thread.sleep(30000);
								}
								wasSent = FileTransferConnection.getInstance()
										.sendFile(logfile);
								if (wasSent)
									lastModifiedFileTime = curFileLastModifiedTime;
							}
							// System.out.println("yoyo");

						}
					}
					Thread.sleep(10000);
				}

			} catch (FileTransferConnectionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (args[0].equals("1")) {

			// if reciever
			try {
				TO_LOG_FOLDER = args[1];
				String ipAddress = args[2];
				FileTransferConnection.getInstance().connect(false, ipAddress);
				File file;
				while (true) {

					file = FileTransferConnection.getInstance().getFile();
					if (file != null)
						System.out.println("Received file: " + file.getName());

				}
			} catch (FileTransferConnectionException e) {
				// TODO Auto-generated catch block 
				e.printStackTrace();
			}
		}

	}
	
	static void errorInArguments() {
		System.out
				.println("Arguments are: \n" +
						"(1) 0/1 - 0 for sender and 1 for receiver,\n" +
						"(2) The folder from which files we'll be downloaded or uploaded according to first argument,\n" +
						"(3) If Sender then the file threshold, that is the file length before sending it \n" +
						"    If the receiver then the ip address of the sender\n" +
						"Example: 0 c:\\from\\ 1048576          (1048576 = 1MB)");
		System.exit(0);

	}
}
